package com.eqdd.yiqidian.ui.yiqiyue;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.YueSearchActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.ui.yiqiyue.fragment.YueFragment;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

/**
 * @author吕志豪 .
 * @date 17-12-21  上午9:35.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class YueSearchActivity extends BaseActivity {
    private YueSearchActivityCustom dataBinding;
    private YueFragment yueFragment;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_yue_search);
        initTopTitleBar(View.VISIBLE, "文章搜索");
    }

    @Override
    public void initData() {
        yueFragment = new YueFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.YUE_SEARCH_ARTICLE_LIST);
        yueFragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment, yueFragment);
        fragmentTransaction.commit();
        RxTextView.textChanges(dataBinding.floatingSearchView.findViewById(R.id.search_bar_text))
                .debounce(1, TimeUnit.SECONDS)
                .subscribe(charSequence -> {
                    if (!TextUtils.isEmpty(charSequence)) {
                        search(charSequence);
                    }
                });
//        dataBinding.floatingSearchView.setOnQueryChangeListener((oldQuery, newQuery) -> {
//            if (!TextUtils.isEmpty(newQuery)) {
//                search(newQuery);
//            }
//        });
        dataBinding.floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {

            }

            @Override
            public void onSearchAction(String currentQuery) {
                if (!TextUtils.isEmpty(currentQuery)) {
                    search(currentQuery);
                }
            }
        });
    }

    @Override
    public void setView() {

    }

    private void search(CharSequence query) {
        yueFragment.search(query);
    }

}
